import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import Qs from 'qs'
// create an axios instance
const requestJson = axios.create({
  // process.env.VUE_APP_BASE_API
  //  baseURL: 'http://192.168.86.131:10001', // url = base url + request url
  baseURL: 'http://119.3.184.15:10000', // 正式环境
  // baseURL: 'http://123.57.59.222:8081/', // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
  transformRequest: [function(data) {
    // 在请求之前对data传参进行格式转换
    data = JSON.stringify(data)

    return data
  }]
})

// request interceptor
requestJson.interceptors.request.use(

  config => {
    // do something before request is sent
    config.headers['Content-Type'] = 'application/json'
    config.headers['Authorization'] = 'Basic ' + btoa('web' + ':' + 'web')// 登陆接口请求固定

    if (sessionStorage.getItem('token') != null) {
      config.headers['Authorization'] = 'Bearer ' + sessionStorage.getItem('token')
    }

    return config
  },
  error => {
    // do something with request error
    // console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
requestJson.interceptors.response.use(
  response => {
    // const res = response.data
    // console.log(res.data)
    return response
  },
  error => {
    Message({
      message: JSON.parse(JSON.stringify(error)).response.data.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default requestJson
